package cz.gillan.housecosts;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import cz.gillan.housecosts.sql.StateDbAdapter;

public class StateEditActivity extends Activity {
	private EditText stateValue;
	private EditText stateDate;
	private EditText stateComment;
	private Button confirmButton;

	static final int DATE_DIALOG_ID = 0;
	    
	private StateDbAdapter stateDbAdapter;
	
	private Long rowId = null;
	private Long gaugeRowId = null;
	private Calendar calendar = Calendar.getInstance();
	

	private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			calendar.set(year, monthOfYear, dayOfMonth);
			updateDateField(calendar);
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		stateDbAdapter = new StateDbAdapter(this);
		stateDbAdapter.open();
		setContentView(R.layout.state_edit);

		stateValue = (EditText) findViewById(R.id.stateValueEdit);
		stateDate = (EditText) findViewById(R.id.stateDateEdit);
		stateComment = (EditText) findViewById(R.id.stateCommentEdit);
		confirmButton = (Button) findViewById(R.id.state_edit_button);
		
		Bundle extras = getIntent().getExtras();
		rowId = (savedInstanceState == null) ? null : (Long) savedInstanceState.getSerializable(StateDbAdapter.KEY_ROWID);
		gaugeRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState.getSerializable(StateDbAdapter.KEY_GAUGEID);
		if (extras != null) {
			rowId = extras.getLong(StateDbAdapter.KEY_ROWID);
			rowId = rowId != -1 ? rowId : null;
			gaugeRowId = extras.getLong(StateDbAdapter.KEY_GAUGEID);
		}

		stateDate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		confirmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				saveState();
				setResult(RESULT_OK);
				finish();
			}
		});
		
        populateFields();
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, dateSetListener, 
					calendar.get(Calendar.YEAR),
					calendar.get(Calendar.MONTH),
					calendar.get(Calendar.DAY_OF_MONTH));
		}
		return null;
	}

	private void populateFields() {
		String value = "";
		String comment = "";
		
		if (rowId != null) {
			Cursor stateCursor = stateDbAdapter.fetchState(rowId);
			startManagingCursor(stateCursor);
			value = stateCursor.getString(stateCursor.getColumnIndexOrThrow(StateDbAdapter.KEY_STATE));
			comment = stateCursor.getString(stateCursor.getColumnIndexOrThrow(StateDbAdapter.KEY_COMMENT));
			Long timeInMillis = stateCursor.getLong(stateCursor.getColumnIndexOrThrow(StateDbAdapter.KEY_DATE)) * 1000l;
			calendar.setTimeInMillis(timeInMillis);
			
		} else {
			calendar = Calendar.getInstance();
		}
		

		stateValue.setText(value);
		stateComment.setText(comment);
		updateDateField(calendar);
	}

	private void saveState() {
		String comment = stateComment.getText().toString();
		String valueString = stateValue.getText().toString();
		Long date = calendar.getTimeInMillis() / 1000l;
		Double value;
		try {
			value = Double.valueOf(valueString);
		} catch (java.lang.NumberFormatException ex) {
			value = 0d;
		}
		
		if (value != 0d) {
			if (rowId == null || rowId == -1) {
				rowId = stateDbAdapter.createNewState(gaugeRowId, date, value, comment);
			} else {
				stateDbAdapter.updateState(rowId, date, value, comment);
			}
		}
	}
	
	private void updateDateField(Calendar c) {
		stateDate.setText(DateFormat.getInstance().format(c.getTime()));
	}
}
